
import { _decorator, Component, Node, TiledMap, Sprite ,resources ,SpriteFrame ,Button ,EventHandler ,log
    ,TiledObjectGroup ,UITransform ,Vec3 ,TiledLayer ,TiledTile} from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = StartGame
 * DateTime = Wed Nov 03 2021 10:30:51 GMT+0800 (中国标准时间)
 * Author = yly1234560
 * FileBasename = StartGame.ts
 * FileBasenameNoExtension = StartGame
 * URL = db://assets/scripts/StartGame.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */
 
@ccclass('StartGame')
export class StartGame extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;


    @property({type:TiledMap})
    private tiledMap;


    start () {
        // [3]


        // 初始化所有棋子
        this.initPieces();
    }

    // update (deltaTime: number) {
    //     // [4]
    // }


    private initPieces(){
        let pieces = this.tiledMap.getObjectGroup('pieces');
        let objects = pieces.getObjects();


        let run_node = this.node.getChildByName("map2");
        let run_tl = run_node.getComponent(TiledLayer);

        const clickEventHandler = new EventHandler();
        clickEventHandler.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler.component = 'StartGame';// 这个是脚本类名
        clickEventHandler.handler = 'piecesOnClick';
        clickEventHandler.customEventData = '';

        // 砖块的大小
        let width = run_tl.getMapTileSize().x;
        let height = run_tl.getMapTileSize().y;

        // 绑定点击事件
        for(let i = 0;i<objects.length;i++) {
            let object = objects[i];
            log(object);
            let tiledTileAt = run_tl.getTiledTileAt(object.x/width,object.y/height,true);
            tiledTileAt.grid = object.gid;
            // tiledTileAt.node.name = "hbc";
            // 层级必须
            tiledTileAt.node.layer = 33554432;

            let ttn = tiledTileAt.node.getComponent(UITransform);
            ttn.setAnchorPoint(0,0);
            ttn.setContentSize(width,height);

            const button= tiledTileAt.node.addComponent(Button);
            button.clickEvents.push(clickEventHandler);
        }


        let tiledTileAt = run_tl.getTiledTileAt(2,1,true);
        tiledTileAt.grid = 41;
        // tiledTileAt.node.name = "hbc";
        // 层级必须
        tiledTileAt.node.layer = 33554432;

        let ttn = tiledTileAt.node.getComponent(UITransform);
        ttn.setAnchorPoint(0,0);
        ttn.setContentSize(width,height);

        const button= tiledTileAt.node.addComponent(Button);
        button.clickEvents.push(clickEventHandler);


    }

    piecesOnClick(event, customEventData){


        let node = event.target;
        let tt = node.getComponent(TiledTile);
        log(node);
        log(tt);

        node.setPosition(0,0);

        tt.x = 0;
        tt.y = 0;
    }


    /******* 陷阱、河道、兽穴 start **********/
    private initBlock(){
        // 陷阱
        let trap = this.node.getChildByName("trap").getComponent(TiledLayer);
        // 兽穴 tiled 未指定 den
        // let childByName1 = this.tiledMap.getChildByName("lay_pieces");
        // 河道
        let river = this.node.getChildByName("block").getComponent(TiledLayer);



        let tiledTiles = river.tiledTiles;
        let tiledTiles_ = trap.tiledTiles;
        let tiles = river.tiles;
        let tiles_ = trap.tiles;
        let tileSet = trap.getTileSet;

        log(tiledTiles);
        log(tiles);
        log(tiledTiles_);
        log(tiles_);

        log(tileSet);



        let tiledTileAt = trap.getTiledTileAt(0,0,true);
        tiledTileAt.x = 0;
        tiledTileAt.grid = 18;
        console.log(tiledTileAt);


        for (let i = 0; i < 7; i++) {
            for (let j = 0; j < 9; j++) {
                console.log("i****j***:"+i+"*****"+j+"***TiledTile"+river.getTiledTileAt(i, j));
                console.log("i****j***:"+i+"*****"+j+"***Position:"+river.getPositionAt(i, j));
                console.log("i****j***:"+i+"*****"+j+"****GID:"+river.getTileGIDAt(i, j));


                // let tiledTileAt = trap.getTileGIDAt(i, j);
                // console.log("i****j***:"+i+"*****"+j+"*******GID:"+tiledTileAt);

                // let tiledTileAt_ = river.getTiledTileAt(i, j);
                // console.log("i****j***:"+i+"*****"+j+"*******GID:"+tiledTileAt_);
            }
        }
    }
    /******* 陷阱、河道、兽穴 end **********/



}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
